﻿using StarPizza.Model.BaseModel;
using StarPizza.Model.Core;
using StarPizza.Model.Ordering;
using StarPizza.Model.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.Model
{
    class ModelMain
    {
        StarDataDataContext db;
        public StarDataDataContext DB { get { return db; } }

        #region User
        UserSpace theUserSpace;
        public UserSpace UserSpace { get { return theUserSpace; } }
        private void InitializeUserSystem(StarDataDataContext db)
        {
            theUserSpace = new UserSpace(db);
        }
        #endregion User

        #region Core
        Stock theStock;
        StockHandler theStockHandler;
        Catalogue theCatalogue;
        CashRegister theCashRegister;
        public IIngredientResource IngredientResource { get { return theStockHandler; } }
        public StockHandler StockHandler { get { return theStockHandler; } }
        public Catalogue Catalogue { get { return theCatalogue; } }
        public CashRegister CashRegister { get { return theCashRegister; } }
        private void InitializeCore(StarDataDataContext db, IUserModeManager userModeManager)
        {
            theStock = new Stock(db);
            theCashRegister = new CashRegister(db);
            theStockHandler = new StockHandler(theStock, theCashRegister);
            theCatalogue = new Catalogue(db, theStockHandler);

        }
        #endregion Core

        #region Ordering
        OrderHistory theOrderHistory;
        OrderProcessor theOrderProcessor;
        public OrderProcessor OrderProcessor { get { return theOrderProcessor; } }
        public OrderHistory OrderHistory { get { return theOrderHistory; } }
        private void InitializeOrdering(StarDataDataContext db, CashRegister cashRegister, StockHandler stockHandler, UserSpace userSpace)
        {
            theOrderHistory = new OrderHistory(db, userSpace);
            theOrderProcessor = new OrderProcessor(theOrderHistory, cashRegister, stockHandler);
        }
        #endregion Ordering
        public ModelMain(string connectionString)
        {
            if (connectionString == "")
                db = new StarDataDataContext();
            else
                db = new StarDataDataContext(connectionString);

            InitializeUserSystem(db);
            InitializeCore(db, theUserSpace);
            InitializeOrdering(db, theCashRegister, theStockHandler, theUserSpace);
        }
    }
}
